/*
 * @lc app=leetcode.cn id=69 lang=javascript
 *
 * [69] Sqrt(x)
 */

// @lc code=start
/**
 * @param {number} x
 * @return {number}
 */
var mySqrt = function (x) {
  let start = 1;
  let end = Math.ceil(x / 2);

  while (start <= end) {
    let mid = Math.floor((start + end) / 2);

    if (mid * mid > x) {
      end = mid - 1;
    } else if (mid * mid < x) {
      start = mid + 1;
    } else {
      return mid;
    }
  }

  return end;
};
// @lc code=end

// @after-stub-for-debug-begin
module.exports = mySqrt;
// @after-stub-for-debug-end
